<?php
/**
 * Created by PhpStorm.
 * User: gyq
 * Date: 2018/10/29
 * Time: 22:40
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck()
    {
        $param = input('param.');
        $res = $this->batch()->check($param);
        if (!$res) {
            $error = $this->getError();
            throw new ParameterException([
                'msg' => $error
            ]);
        } else {
            return true;
        }
    }

    protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function getDataByRule($arrays)
    {
        if (array_key_exists('id', $arrays) || array_key_exists('uid', $arrays)) {
            throw new ParameterException([
                'msg' => '参数中包含有非法的参数名user_id或者id'
            ]);
        }
        $newArray = [];
        foreach ($this->rule as $key => $value) {
            if (array_key_exists($key, $arrays)) {
                $newArray[$key] = $arrays[$key];
            }
        }
        return $newArray;
    }

    protected function isNotEmpty($value, $rule = '', $data = '', $field = '')
    {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }
    }

    protected function isMobile($value, $rule = '', $data = '', $field = '')
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if($result){
            return true;
        } else {
            return false;
        }
    }
}